package kr.pe.johnkim.waiting.mobile.controller;

import kr.pe.johnkim.waiting.mobile.domain.WaitingMemberRecommendation;
import kr.pe.johnkim.waiting.mobile.domain.WaitingRecommendation;
import kr.pe.johnkim.waiting.mobile.service.WaitingMemberRecommendationService;
import kr.pe.johnkim.waiting.mobile.service.WaitingRecommendationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Waiting 추천 관련 controller class 이다.
 *
 * @author : John_Kim <cale9797@gmail.com>
 * @date : 13. 6. 17.
 */
@Controller
@RequestMapping(value = "/waitingRecommendationController")
public class WaitingRecommendationController {

    // SLF4J Logging
    private Logger logger = LoggerFactory.getLogger(WaitingRecommendationController.class);

    @Autowired
    private WaitingRecommendationService waitingRecommendationService;

    @Autowired
    private WaitingMemberRecommendationService waitingMemberRecommendationService;

    /**
     * 사용자의 추천 중복을 확인 한다.
     *
     * @param waitingRecommendation
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/selectDuplicationRecommendation", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public WaitingRecommendation selectDuplicationRecommendation(@RequestBody WaitingRecommendation waitingRecommendation) throws Exception {
        logger.info("selectDuplicationRecommendation");
        return this.waitingRecommendationService.selectDuplicationRecommendation(waitingRecommendation);
    }

    /**
     * 추천을 등록 한다.
     *
     * @param waitingRecommendation
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/registerRecommendation", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public WaitingRecommendation registerRecommendation(@RequestBody WaitingRecommendation waitingRecommendation) throws Exception {
        logger.info("registerRecommendation");

        logger.info("> merchantId        : {}", waitingRecommendation.getMerchantId());
        logger.info("> creator           : {}", waitingRecommendation.getCreator());
        logger.info("> totalMember       : {}", waitingRecommendation.getTotalMember());
        logger.info("> totalEvaluation   : {}", waitingRecommendation.getTotalEvaluation());
        logger.info("> averageEvaluation : {}", waitingRecommendation.getAverageEvaluation());
        logger.info("> activateYn        : {}", waitingRecommendation.getActivateYn());
        logger.info("> modifyDate        : {}", waitingRecommendation.getModifyDate());
        logger.info("> registerDate      : {}", waitingRecommendation.getRegisterDate());

        WaitingRecommendation result = new WaitingRecommendation();

        try {
            this.waitingRecommendationService.registerRecommendation(waitingRecommendation);
            result.setResult("SUCCESS");
            logger.info("success to register waitingRecommendation.");
        } catch (Exception e) {
            result.setResult("FAIL");
            logger.error("fail to register waitingRecommendation.");
        }

        return result;
    }

    /**
     * 추천을 업데이트 한다.
     *
     * @param waitingRecommendation
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/updateRecommendation", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public WaitingRecommendation updateRecommendation(@RequestBody WaitingRecommendation waitingRecommendation) throws Exception {
        logger.info("updateRecommendation");

        WaitingRecommendation result = new WaitingRecommendation();

        try {
            this.waitingRecommendationService.updateRecommendation(waitingRecommendation);
            result.setResult("SUCCESS");
            logger.info("success to update recommendation.");
        } catch (Exception e) {
            result.setResult("FAIL");
            logger.error("fail to update recommendation.");
        }

        return result;
    }

    /**
     * 추천을 비활성화 한다.
     *
     * @param waitingRecommendation
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/updateDeactivateRecommendation", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public WaitingRecommendation updateDeactivateRecommendation(@RequestBody WaitingRecommendation waitingRecommendation) throws Exception {
        logger.info("updateDeactivateRecommendation");

        WaitingRecommendation result = new WaitingRecommendation();

        try {
            this.waitingRecommendationService.updateDeactivateRecommendation(waitingRecommendation);
            result.setResult("SUCCESS");
            logger.info("success update deactivate waitingRecommendation.");
        } catch (Exception e) {
            result.setResult("FAIL");
            logger.error("fail to update deactivate waitingRecommendation.");
        }

        return result;
    }

    /**
     * 검색한 가맹점에 대한 추천 목록을 가져 온다.
     *
     * @param waitingRecommendation
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/selectSearchRecommendationList", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public List<WaitingRecommendation> selectSearchRecommendationList(@RequestBody WaitingRecommendation waitingRecommendation) throws Exception {
        logger.info("selectSearchRecommendationList");
        return this.waitingRecommendationService.selectSearchRecommendationList(waitingRecommendation);
    }

    /**
     * 특정 가맹점에 대한 추천 정보를 가져 온다.
     *
     * @param waitingRecommendation
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/selectRecommendationInformation", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public WaitingRecommendation selectRecommendationInformation(@RequestBody WaitingRecommendation waitingRecommendation) throws Exception {
        logger.info("selectRecommendationTotalCount");
        return this.waitingRecommendationService.selectRecommendationInformation(waitingRecommendation);
    }
}
